package pl.prv.polanie.client.panels.lists;

import pl.prv.polanie.client.dialogs.CreateSubgroupDialog;
import pl.prv.polanie.client.dto.CSSubgroupDTO;
import pl.prv.polanie.client.dto.CSVSubgroupDTO;
import pl.prv.polanie.client.model.Cache;
import pl.prv.polanie.client.model.ViewDataProvider;
import pl.prv.polanie.client.widgets.table.TableSubgroups;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;

public class SubgroupsList extends ObjectsList {

	
	TableSubgroups tableSubgroups = null;  

	public SubgroupsList(final long groupID) {
		tableSubgroups = new TableSubgroups(groupID, this);
		
		
		CSSubgroupDTO[] ss;
		
		if (groupID != -1) {
			ss = Cache.getSubgroupsByGroupID(groupID);
		} else {
			ss = Cache.getAllSubgroups();
		}
			
		add(tableSubgroups);
		
		
		for (int i = 0; i < ss.length; i++) {
			
			CSSubgroupDTO cs = ss[i];
			CSVSubgroupDTO csv = ViewDataProvider.getCSVSubgroup(cs);
			
			tableSubgroups.addRow(cs, csv);
		}
		
		
		addRecordButton.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {
				CreateSubgroupDialog csd = new CreateSubgroupDialog(groupID, SubgroupsList.this, null);
				csd.center();
			}
		});
		
		removeSelectedRecordButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				tableSubgroups.removeSelectedSubgroupFromTable();
			}
		});

		setupButtons();
	}

	
	public void addCSSubgroup(CSSubgroupDTO csSubgroup) {
		tableSubgroups.saveOrUpdateSubgroupInTable(csSubgroup);
	}
	
	public void removeSelectedCSSubgroup() {
		tableSubgroups.removeSelectedSubgroupFromTable();
	}
	

	
}
